package com.winit.openapi.ups.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.erp.controller.DeliveryWayController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ups.spi.conversion.ConversionService;
import com.winit.ups.spi.conversion.command.QueryConversionConfigCommand;
import com.winit.ups.spi.conversion.vo.ConversionConfigVo;


/**
 * ups2中的货币 <Change to the actual description of this class>
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * chuanwei.luo 	1.0  		2017年4月20日 	Created
 * 
 * </pre>
 * @since 1.
 */


@Controller
@RequestMapping(value = "/ups/conversion")
public class ConversionController extends BaseController {
	
	
    private Logger               logger = LoggerFactory.getLogger(DeliveryWayController.class);
	
    @Resource
    private ConversionService  conversionService;
    
    /**
     * 汇率获取配置查询
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/conversionConfig")
    public String listProvinceByCountryCode(@ModelAttribute RequestMsg requestMsg,
                                            @ModelAttribute ResponseMsg responseMsg) {
    	
          logger.info("汇率获取配置查询的请求参数"+requestMsg.getData().toString());
    	   Object params = requestMsg.getData();
           JSONObject json = null;
           if (params instanceof JSONObject) {
               json = (JSONObject) params;
           }
           QueryConversionConfigCommand command = new QueryConversionConfigCommand();
           command.setCtx(CommandContext.getContext());
           if(json!=null){     
			// 获取参数
			String currencyCode = (String) json.get("currencyCode");
			String currencyToCode = (String) json.get("currencyToCode");

			if (currencyCode != null && !("").equals(currencyCode)) {
				command.setCurrencyCode(currencyCode);
			}
			if (currencyToCode != null && !("").equals(currencyToCode)) {
				command.setCurrencyToCode(currencyToCode);
			}
           }
		Page<ConversionConfigVo> conversionConfigs = conversionService.pageConversionConfig(command);
		Map<String, Object> data = new HashMap<String, Object>();
		if (null != conversionConfigs) {
			List<ConversionConfigVo> list = conversionConfigs.content;
			//原来的数据中存在重复的目标币种，对于同一个源币种和目标币种的数据，任意取一条
	        ridRepeateed(list);
			data.put("list", list);
		}
		responseMsg.setData(data);
		logger.info("汇率获取配置共有" + conversionConfigs.content.size() + "条");
		return SUCCESS;
    }
    
    public void ridRepeateed(List<ConversionConfigVo> list ){
    	//对于目标币种去重
        if(list!=null&&list.size()>0){
        	Map<String,ConversionConfigVo> map=new HashMap<String,ConversionConfigVo>();
        	for(ConversionConfigVo conversionConfigVo:list){
        		String key=conversionConfigVo.getCurrencyCode()+"-"+conversionConfigVo.getCurrencyToCode();
        		map.put(key, conversionConfigVo);	
        	}
        	list.clear();
        	list.addAll(map.values());
        }
    }

    
    
    
}
